package cn.bocweb.spinning.view.dialog;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import cn.bocweb.spinning.R;
import cn.bocweb.spinning.view.common.TimeTick;

/**
 * Created by chenchangxing on 15/12/23.
 */
public class TestDialog extends AlertDialog {

    OnClickListener onClickListener;
    Button btnSubmit;
    Button btnCancel;
    TextView dialogRight;
    TextView dialogWrong;
    TextView dialogMiss;
    TextView dialogDo;
    LinearLayout linearLayoutDo;
    LinearLayout linearLayoutRightFalse;
    boolean isTimeLimit;
    String right;
    String wrong;
    String miss;
    String go;
    TimeTick timeTick;
    String btnText;
    boolean isMatch;

    public TestDialog(Context context, boolean isMatch) {
        super(context);
        this.isMatch = isMatch;
    }

    public TestDialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    public void setTimeLimit(boolean timeLimit) {
        isTimeLimit = timeLimit;
    }

    public void setRight(String right) {
        this.right = right;
    }

    public void setWrong(String wrong) {
        this.wrong = wrong;
    }

    public void setMiss(String miss) {
        this.miss = miss;
    }

    public void setGo(String go) {
        this.go = go;
    }

    public void setBtnText(String btnText) {
        this.btnText = btnText;
    }

    public void setTimeTick(TimeTick timeTick) {
        this.timeTick = timeTick;
    }

    public void setOnClickListener (OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_test);
        btnSubmit = (Button)findViewById(R.id.btn_submit);
        btnCancel = (Button)findViewById(R.id.btn_cancel);
        dialogRight = (TextView)findViewById(R.id.test_dialog_right);
        dialogWrong = (TextView)findViewById(R.id.test_dialog_wrong);
        dialogMiss = (TextView)findViewById(R.id.test_dialog_miss);
        linearLayoutDo = (LinearLayout)findViewById(R.id.linearLayout_do);
        linearLayoutRightFalse = (LinearLayout)findViewById(R.id.linearLayout_right_false);
        dialogDo = (TextView)findViewById(R.id.test_dialog_do);
    }

    @Override
    protected void onStart() {
        super.onStart();

//        timeTick.cancel();

        if (isTimeLimit) {
            btnSubmit.setVisibility(View.GONE);
            setCancelable(false);
        } else {
            btnSubmit.setVisibility(View.VISIBLE);
            setCancelable(true);
        }

        btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        dialogMiss.setText(miss);
        btnSubmit.setText(btnText);
        if (isMatch) {
            dialogDo.setText(String.valueOf(Integer.parseInt(right) + Integer.parseInt(wrong)));
            linearLayoutDo.setVisibility(View.VISIBLE);
            linearLayoutRightFalse.setVisibility(View.GONE);
        } else {
            linearLayoutDo.setVisibility(View.GONE);
            linearLayoutRightFalse.setVisibility(View.VISIBLE);
            dialogRight.setText(right);
            dialogWrong.setText(wrong);
        }

        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onClickListener.click(v);
            }
        });
    }

    @Override
    protected void onStop() {
        super.onStop();
        onClickListener.submit();
    }

    public interface OnClickListener {
        void click(View v);

        void submit();
    }
}
